package net.util;

import java.io.File;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;


/**
 * 
 * @author paul
 *
 */
public class File_Handler {
	static public BufferedReader createBufferReader( String filePath ){	
		//Debug.println("createBufferReader for : " + filePath);
		
		BufferedReader buffReader	= null;
		
		try {
			FileReader fileReader;
			fileReader = new FileReader( filePath );
			buffReader = new BufferedReader( fileReader );
		} catch (FileNotFoundException e) {
			System.err.print( e.toString() );			
		}
		
		return buffReader;
	}
	
	/**
	 * 
	 * @param filePath
	 * @return Each element in the returned ArrayList is one line string in the source file.
	 */
	static public ArrayList<String> fileContentToStrings( String filePath ){
		ArrayList<String> fileContentStrings = new ArrayList<String>();
		
		try {	
			BufferedReader bufferReader = File_Handler.createBufferReader( filePath );
			while ( true ){
				String oneLine = bufferReader.readLine();
			
				if ( oneLine != null )
					fileContentStrings.add( oneLine.trim() );
				else
					break;
			}
					
		}catch( Exception e ){
			System.err.print( "write new format: " + e );
		}
		
		return fileContentStrings;
	}
	
	/**
	 * Write the specified text to the specified file at the beginning
	 * @param file_name
	 * @param text
	 * @throws IOException
	 */
	static public void write_to_file( String file_name, String text ) throws IOException{
		write_to_file( new File( file_name ), text );
	}
	
	static public void write_to_file( File file, String text) throws IOException{
		FileWriter writer	= new FileWriter( file , false );

		for ( int i = 0 ; i < text.length(); i++){
			int ch = (int)text.charAt( i );
			writer.write( ch );
		}	

		//writer.write( '\n' );

		writer.flush();
		writer.close();
	}
	
//	append to a file
	static public void append_to_file( String file_name, String text ) throws IOException{
		append_to_file( new File( file_name ), text );
	}
	
	
	/**
	 * @param file
	 * @param text
	 * @throws IOException
	 */
	static public void append_to_file( File file, String text ) throws IOException{
		/*if (file.canWrite() ){
			System.out.println( file + " can write."  + text);
		}else{
			System.out.println( file + " can NOT write." + text);
		}*/


		FileWriter writer	= new FileWriter( file , true );

		for ( int i = 0 ; i < text.length(); i++){
			int ch = (int)text.charAt( i );
			writer.write( ch );
		}	

		writer.write( '\n' );

		writer.flush();
		writer.close();
	}

	static public void batch_append_to_file( String file_name, Collection texts ) throws IOException{
		for ( Iterator _i = texts.iterator(); _i.hasNext(); ){
			String a_text_line	= (String)_i.next();

			append_to_file( file_name, a_text_line );
		}
	}
	
	static public boolean delete_file( String file_name ){
		return (new File( file_name )).delete();
	}
}
